package com.wyix.controller;

import com.wyix.entity.User;
import com.wyix.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/users/**")
public class UserController {
    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    // 列表页
    @GetMapping("/")
    public String list(Model model) {
        model.addAttribute("users", userService.getAllUsers());
        return "user/list";
    }

    // 添加用户（表单页）
    @GetMapping("/add")
    public String addForm() {
        return "user/add";
    }

    // 添加用户（提交）
    @PostMapping("/add")
    public String addUser(User user) {
        userService.addUser(user);
        return "redirect:/users";
    }

    // 删除用户
    @GetMapping("/delete")
    public String deleteUser(@RequestParam Integer id) {
        userService.deleteUser(id);
        return "redirect:/users";
    }
}
